<template>
  <div class='container'>
    <el-dialog
      :title="row.id?'编辑文章':'新增文章'"
      :visible.sync="addDialogVisible"
      width="40%"
      :before-close="handleClose">
      <el-form ref="form" :model="addForm" :rules="addFormRules" label-width="80px">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="addForm.title"></el-input>
        </el-form-item>
        <el-form-item  label="文章内容" prop="articleBody">
          <!-- 富文本编辑器 -->
           <quill-editor
              :options="editorOptions"
              v-model="addForm.articleBody"
            ></quill-editor>
        </el-form-item>
        <el-form-item class="videoUrl" label="视频地址" prop="videoURL">
          <el-input v-model="addForm.url"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="onSave">确 认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import hljs from 'highlight.js'
import 'highlight.js/styles/monokai-sublime.css'
import { add, update } from '@/api/hmmm/articles.js'
import { Message } from 'element-ui'

export default {
  name: 'addArticles',
  model: {
    prop: 'addDialogVisible'
  },
  props: {
    addDialogVisible: {
      type: Boolean,
      required: true
    },
    row: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      addForm: {
        title: this.row.id ? this.row.title : '',
        articleBody: this.row.articleBody ? this.row.articleBody : '',
        videoURL: this.row.videoURL ? this.row.videoURL : ''
      },
      // 表单校验
      addFormRules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      },
      editorOptions: {
        placeholder: '',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ],
          // 语法高亮
          syntax: {
            highlight: (text) => hljs.highlightAuto(text).value
          }
        }
      }
    }
  },
  watch: {
    row: {
      handler (val) {
        this.addForm.title = val === null ? '' : val.title
        this.addForm.articleBody = val === null ? '' : val.articleBody
        this.addForm.videoURL = val === null ? '' : val.videoURL
      }
    }
  },
  created () {

  },

  methods: {
    // 对话框关闭
    handleClose () {
      // 重置表单
      this.$refs.form.resetFields()
      this.$emit('reset')
      this.$emit('input', false)
    },
    // 点击确定提交新增
    async onSave () {
      // 表单验证
      // await this.$refs.form.validate()
      if (this.row.id) {
        // 编辑
        await update({
          ...this.addForm,
          id: this.row.id
        })
        this.$message.success('修改成功')
        this.$emit('edit-success')
      } else {
        // 验证通过添加
        await this.$refs.form.validate()
        await add(this.addForm)
        Message.success('添加成功')
        this.$emit('add-success')
      }
      this.handleClose()
    }
  }
}
</script>

<style scoped lang='less'>
.quill-editor{
  height: 200px;
  margin-bottom: 20px;
}
/deep/.videoUrl{
  margin-top: 60px;
}
</style>
